package football.haha.com.footballtv.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunfusheng.marqueeview.MarqueeView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import football.haha.com.footballtv.FootballApplication;
import football.haha.com.footballtv.R;
import football.haha.com.footballtv.bean.BeanAd;
import football.haha.com.footballtv.bean.BeanIndexFight;
import football.haha.com.footballtv.bean.BeanIndexFootball;
import football.haha.com.footballtv.utils.ActivityUtils;
import football.haha.com.footballtv.utils.ApiUtils;
import football.haha.com.footballtv.utils.ImageToos;
import football.haha.com.footballtv.utils.NetUtils;
import football.haha.com.footballtv.utils.Tools;
import football.haha.com.footballtv.view.BorderView;
import football.haha.com.footballtv.view.SmoothHorizontalScrollView;

public class MainActivity extends BaseActivity implements View.OnFocusChangeListener {

    @Bind(R.id.tv_ad)
    MarqueeView tvAd;
    @Bind(R.id.fl_top_check)
    FrameLayout flTopCheck;
    @Bind(R.id.rb_me)
    CheckBox rbMe;
    @Bind(R.id.rb_football)
    CheckBox rbFootball;
    @Bind(R.id.rb_fight)
    CheckBox rbFight;
    @Bind(R.id.ll_meal)
    LinearLayout llMeal;
    @Bind(R.id.ll_record)
    LinearLayout llRecord;
    @Bind(R.id.ll_guest)
    LinearLayout llGuest;
    @Bind(R.id.ll_business)
    LinearLayout llBusiness;
    @Bind(R.id.layout_me)
    RelativeLayout layoutMe;
    @Bind(R.id.iv_football_league1)
    ImageView ivFootballLeague1;
    @Bind(R.id.iv_football_league2)
    ImageView ivFootballLeague2;
    @Bind(R.id.iv_football_league3)
    ImageView ivFootballLeague3;
    @Bind(R.id.iv_football_league4)
    ImageView ivFootballLeague4;
    @Bind(R.id.iv_football_league5)
    ImageView ivFootballLeague5;
    @Bind(R.id.iv_football_league6)
    ImageView ivFootballLeague6;
    @Bind(R.id.iv_football_league7)
    ImageView ivFootballLeague7;
    @Bind(R.id.iv_football_league8)
    ImageView ivFootballLeague8;
    @Bind(R.id.iv_football_league9)
    ImageView ivFootballLeague9;
    @Bind(R.id.fl_football_league_more)
    FrameLayout flFootballLeagueMore;
    @Bind(R.id.iv_football_rec_big)
    ImageView ivFootballRecBig;
    @Bind(R.id.iv_football_rec1)
    ImageView ivFootballRec1;
    @Bind(R.id.iv_football_rec2)
    ImageView ivFootballRec2;
    @Bind(R.id.ll_football_game_info1)
    LinearLayout llFootballGameInfo1;
    @Bind(R.id.ll_football_game_info2)
    LinearLayout llFootballGameInfo2;
    @Bind(R.id.ll_football_game_info3)
    LinearLayout llFootballGameInfo3;
    @Bind(R.id.tv_football_more_game)
    TextView tvFootballMoreGame;
    @Bind(R.id.layout_football)
    SmoothHorizontalScrollView layoutFootball;
    @Bind(R.id.iv_fight_rec_big)
    ImageView ivFightRecBig;
    @Bind(R.id.tv_fight_tip1)
    TextView tvFightTip1;
    @Bind(R.id.tv_fight_title1)
    TextView tvFightTitle1;
    @Bind(R.id.tv_fight_tip2)
    TextView tvFightTip2;
    @Bind(R.id.tv_fight_title2)
    TextView tvFightTitle2;
    @Bind(R.id.tv_fight_tip3)
    TextView tvFightTip3;
    @Bind(R.id.tv_fight_title3)
    TextView tvFightTitle3;
    @Bind(R.id.ll_fight_info1)
    LinearLayout llFightInfo1;
    @Bind(R.id.ll_fight_info2)
    LinearLayout llFightInfo2;
    @Bind(R.id.ll_fight_info3)
    LinearLayout llFightInfo3;
    @Bind(R.id.tv_more_fight)
    TextView tvMoreFight;
    @Bind(R.id.fl_football_content)
    FrameLayout flFootballContent;
    @Bind(R.id.fl_fight_content)
    FrameLayout flFightContent;
    @Bind(R.id.layout_fight)
    SmoothHorizontalScrollView layoutFight;


    @Bind(R.id.rl_main)
    RelativeLayout rlMain;

    //中间的赛事推荐
    @Bind(R.id.tv_football_rec1_type)
    TextView tvFootballRec1Type;
    @Bind(R.id.iv_football_rec1_right_icon)
    ImageView ivFootballRec1RightIcon;
    @Bind(R.id.tv_football_rec1_right_name)
    TextView tvFootballRec1RightName;
    @Bind(R.id.iv_football_rec_tip)
    ImageView ivFootballRecTip;
    @Bind(R.id.rl_football_rec1)
    RelativeLayout rlFootballRec1;
    @Bind(R.id.iv_football_rec2_left_icon)
    ImageView ivFootballRec2LeftIcon;
    @Bind(R.id.tv_football_rec2_left_name)
    TextView tvFootballRec2LeftName;
    @Bind(R.id.tv_football_rec2_time)
    TextView tvFootballRec2Time;
    @Bind(R.id.tv_football_rec2_type)
    TextView tvFootballRec2Type;
    @Bind(R.id.iv_football_rec2_right_icon)
    ImageView ivFootballRec2RightIcon;
    @Bind(R.id.tv_football_rec2_right_name)
    TextView tvFootballRec2RightName;
    @Bind(R.id.iv_football_rec_tip2)
    ImageView ivFootballRecTip2;
    @Bind(R.id.rl_football_rec2)
    RelativeLayout rlFootballRec2;
    @Bind(R.id.iv_football_rec3_left_icon)
    ImageView ivFootballRec3LeftIcon;
    @Bind(R.id.tv_football_rec3_left_name)
    TextView tvFootballRec3LeftName;
    @Bind(R.id.tv_football_rec3_time)
    TextView tvFootballRec3Time;
    @Bind(R.id.tv_football_rec3_type)
    TextView tvFootballRec3Type;
    @Bind(R.id.iv_football_rec3_right_icon)
    ImageView ivFootballRec3RightIcon;
    @Bind(R.id.tv_football_rec3_right_name)
    TextView tvFootballRec3RightName;
    @Bind(R.id.iv_football_rec_tip3)
    ImageView ivFootballRecTip3;
    @Bind(R.id.rl_football_rec3)
    RelativeLayout rlFootballRec3;
    @Bind(R.id.tv_football_rec_big_title)
    TextView tvFootballRecBigTitle;
    @Bind(R.id.fl_football_rec_big)
    FrameLayout flFootballRecBig;
    @Bind(R.id.iv_football_rec1_left_icon)
    ImageView ivFootballRec1LeftIcon;
    @Bind(R.id.iv_fight_rec_tip1)
    ImageView ivFightRecTip1;
    @Bind(R.id.iv_fight_rec_tip2)
    ImageView ivFightRecTip2;
    @Bind(R.id.iv_fight_rec_tip3)
    ImageView ivFightRecTip3;
    @Bind(R.id.tv_football_rec1_left_name)
    TextView tvFootballRec1LeftName;
    @Bind(R.id.iv_fight_rec1_left_icon)
    ImageView ivFightRec1LeftIcon;
    @Bind(R.id.tv_fight_rec1_left_name)
    TextView tvFightRec1LeftName;
    @Bind(R.id.tv_fight_rec1_time)
    TextView tvFightRec1Time;
    @Bind(R.id.tv_fight_rec1_type)
    TextView tvFightRec1Type;
    @Bind(R.id.iv_fight_rec1_right_icon)
    ImageView ivFightRec1RightIcon;
    @Bind(R.id.tv_fight_rec1_right_name)
    TextView tvFightRec1RightName;
    @Bind(R.id.iv_fight_rec2_left_icon)
    ImageView ivFightRec2LeftIcon;
    @Bind(R.id.tv_fight_rec2_left_name)
    TextView tvFightRec2LeftName;
    @Bind(R.id.tv_fight_rec2_time)
    TextView tvFightRec2Time;
    @Bind(R.id.tv_fight_rec2_type)
    TextView tvFightRec2Type;
    @Bind(R.id.iv_fight_rec2_right_icon)
    ImageView ivFightRec2RightIcon;
    @Bind(R.id.tv_fight_rec2_right_name)
    TextView tvFightRec2RightName;
    @Bind(R.id.iv_fight_rec3_left_icon)
    ImageView ivFightRec3LeftIcon;
    @Bind(R.id.tv_fight_rec3_left_name)
    TextView tvFightRec3LeftName;
    @Bind(R.id.tv_fight_rec3_time)
    TextView tvFightRec3Time;
    @Bind(R.id.tv_fight_rec3_type)
    TextView tvFightRec3Type;
    @Bind(R.id.iv_fight_rec3_right_icon)
    ImageView ivFightRec3RightIcon;
    @Bind(R.id.tv_fight_rec3_right_name)
    TextView tvFightRec3RightName;
    @Bind(R.id.tv_fight_rec_big_title)
    TextView tvFightRecBigTitle;
    @Bind(R.id.fl_fight_rec_big)
    FrameLayout flFightRecBig;
    @Bind(R.id.ll_fight1)
    LinearLayout llFight1;
    @Bind(R.id.ll_fight2)
    LinearLayout llFight2;
    @Bind(R.id.ll_fight3)
    LinearLayout llFight3;
    @Bind(R.id.tv_football_rec1_title)
    TextView tvFootballRec1Title;
    @Bind(R.id.fl_football_rec1)
    FrameLayout flFootballRec1;
    @Bind(R.id.tv_football_rec2_title)
    TextView tvFootballRec2Title;
    @Bind(R.id.fl_football_rec2)
    FrameLayout flFootballRec2;
    @Bind(R.id.tv_football_rec1_time)
    TextView tvFootballRec1Time;
    @Bind(R.id.rl_fight_rec1)
    RelativeLayout rlFightRec1;
    @Bind(R.id.rl_fight_rec2)
    RelativeLayout rlFightRec2;
    @Bind(R.id.rl_fight_rec3)
    RelativeLayout rlFightRec3;

    private List<ImageView> leagueIVList = new ArrayList();
    private List<View> footRecContentList = new ArrayList();
    private List<View> fightRecContentList = new ArrayList();
    private List<BeanIndexFootball.LiveHallBean.ListBeanX> liveList = new ArrayList<>();
    private ArrayList<BeanIndexFootball.LeagueMatchListBean.ListBean> leagueList = null;

    private BeanIndexFootball beanIndexFootball;
    private BeanIndexFight beanIndexFight;
    private BeanAd beanAd;

    private boolean firstShowFight = true;
    private BorderView border;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        border = new BorderView(this);
        border.setBackgroundResource(R.drawable.focus_rec_home);
        border.attachTo(flFootballContent);
        border.attachTo(flTopCheck);
        border.attachTo(layoutMe);
        border.attachTo(flFightContent);

        createBorderView(rlMain);

        initView();
        layoutFootball.setFadingEdge((int) getResources().getDimension(R.dimen.px260));

        getIndexFootball();
        getIndexGrapple();
        getAdData();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onBackPressed() {
        showDialogExit();
    }

    public void showDialogExit() {
        final Dialog singeLeftRightTipDialog = new Dialog(this, R.style.MyDialog);
        ImageView ivRec = (ImageView) singeLeftRightTipDialog.findViewById(R.id.iv_rec);
        if (liveList != null && liveList.size()>0){
            ImageToos.setImageWithUrl(liveList.get(0).getLivePicUrl(), ivRec, FootballApplication.getOptions(R.drawable.default_video));
        }
        singeLeftRightTipDialog.setContentView(R.layout.dialog_exit);
        singeLeftRightTipDialog.findViewById(R.id.fl_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (singeLeftRightTipDialog != null) {
                    singeLeftRightTipDialog.dismiss();
                }
                finish();
            }
        });

        View view = singeLeftRightTipDialog.findViewById(R.id.fl_cancel);
        view.requestFocus();
        singeLeftRightTipDialog.findViewById(R.id.fl_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (singeLeftRightTipDialog != null) {
                    singeLeftRightTipDialog.dismiss();
                }
            }
        });

        RelativeLayout warp = (RelativeLayout) singeLeftRightTipDialog.findViewById(R.id.fl_dialog);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(Tools.getDisplayWidth(this), Tools.getDisplayHeight(this));
        warp.setLayoutParams(lp);

        singeLeftRightTipDialog.show();

    }

    private void initView() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                rbFight.requestFocus();
            }
        }, 200);
        showViewByHandler(rbFootball);
        layoutFight.setFadingEdge((int) getResources().getDimension(R.dimen.px100));
        layoutFootball.setFadingEdge((int) getResources().getDimension(R.dimen.px100));

        leagueIVList.add(ivFootballLeague1);
        leagueIVList.add(ivFootballLeague2);
        leagueIVList.add(ivFootballLeague3);
        leagueIVList.add(ivFootballLeague4);
        leagueIVList.add(ivFootballLeague5);
        leagueIVList.add(ivFootballLeague6);
        leagueIVList.add(ivFootballLeague7);
        leagueIVList.add(ivFootballLeague8);
        leagueIVList.add(ivFootballLeague9);

    }

    private void getIndexFootball() {
        Map<String, String> params = new HashMap<>();
        params.put("command", "index_football");
        params.put("busCode", "IPS");
        NetUtils.getData(this, ApiUtils.HOST_URL, ApiUtils.TAGIndex_football, params, BeanIndexFootball.class, null, this);
    }

    private void getIndexGrapple() {
        Map<String, String> params = new HashMap<>();
        params.put("command", "index_grapple");
        params.put("busCode", "IPS");
        NetUtils.getData(this, ApiUtils.HOST_URL, ApiUtils.TAGIndex_grapple, params, BeanIndexFight.class, null, this);
    }

    @Override
    public void onFocusChange(View view, boolean b) {
        if (b) {
            switch (view.getId()) {
                case R.id.rb_me:
                    scale = 1.0f;
                    borderView.setBorderBitmapResId(R.drawable.focus_tab, (int) getResources().getDimension(R.dimen.px100));
                    rbMe.setChecked(true);
                    rbFootball.setChecked(false);
                    rbFight.setChecked(false);
                    layoutMe.setVisibility(View.VISIBLE);
                    layoutFight.setVisibility(View.GONE);
                    layoutFootball.setVisibility(View.GONE);
                    break;
                case R.id.rb_football:
                    scale = 1.0f;
                    borderView.setBorderBitmapResId(R.drawable.focus_tab, (int) getResources().getDimension(R.dimen.px100));
                    rbMe.setChecked(false);
                    rbFootball.setChecked(true);
                    rbFight.setChecked(false);
                    layoutMe.setVisibility(View.GONE);
                    layoutFight.setVisibility(View.GONE);
                    layoutFootball.setVisibility(View.VISIBLE);
                    break;
                case R.id.rb_fight:
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            firstShowFight = false;
                        }
                    }, 700);
                    scale = 1.0f;
                    borderView.setBorderBitmapResId(R.drawable.focus_tab, (int) getResources().getDimension(R.dimen.px100));
                    rbMe.setChecked(false);
                    rbFootball.setChecked(false);
                    rbFight.setChecked(true);
                    layoutMe.setVisibility(View.GONE);
                    layoutFight.setVisibility(View.VISIBLE);
                    layoutFootball.setVisibility(View.GONE);
                    break;
            }
        }
        super.onFocusChange(view, b);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            if (focusView != null) {
                if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
                    if (focusView == llBusiness) {
                        rbFootball.requestFocus();
                        ivFootballLeague1.requestFocus();
                        return true;
                    } else if (footRecContentList.contains(focusView)) {
                        if (footRecContentList.indexOf(focusView) > (footRecContentList.size() - 3)) {
                            rbFight.requestFocus();
                            if (firstShowFight) {
                                showViewByHandler(flFightRecBig);
                            } else {
                                flFightRecBig.requestFocus();
                            }
                            return true;
                        }
                    } else if (footRecContentList.size() == 0 && (rlFootballRec1 == focusView || rlFootballRec2 == focusView || rlFootballRec3 == focusView
                            || tvFootballMoreGame == focusView)) {
//                        rbFight.requestFocus();
//                        if (firstShowFight) {
//                            showViewByHandler(flFightRecBig);
//                        } else {
//                            flFightRecBig.requestFocus();
//                        }
                        return true;
                    }
                } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
                    if (focusView == ivFootballLeague1 || focusView == ivFootballLeague3 || focusView == ivFootballLeague5
                            || focusView == ivFootballLeague7 || focusView == ivFootballLeague9) {
                        rbMe.requestFocus();
                        llBusiness.requestFocus();
                        return true;
                    } else if (focusView == flFightRecBig || focusView == llFight1 || focusView == llFight2
                            || focusView == llFight3) {
                        rbFootball.requestFocus();
                        if (footRecContentList.size() > 0) {
                            footRecContentList.get(footRecContentList.size() - 1).requestFocus();
                        } else {
                            rlFootballRec1.requestFocus();
                        }
                        return true;
                    }

                }
            }

        }
        return super.dispatchKeyEvent(event);
    }

    @OnClick({R.id.fl_football_rec_big, R.id.fl_football_rec1, R.id.fl_football_rec2, R.id.iv_fight_rec_big, R.id.fl_fight_rec_big,
            R.id.ll_fight1, R.id.ll_fight2, R.id.ll_fight3, R.id.fl_football_league_more, R.id.tv_more_fight,
            R.id.ll_meal, R.id.ll_record, R.id.ll_guest, R.id.ll_business, R.id.tv_ad})
    public void onMainClick(View view) {
        switch (view.getId()) {
            case R.id.tv_ad:
                showToast(tvAd.getPosition() + "  点击位置啊");
                break;
            case R.id.fl_football_rec_big: {
                if (liveList.size() > 0) {
                    BeanIndexFootball.LiveHallBean.ListBeanX live = liveList.get(0);
                    getAuthPlayUrl(live.getName(), beanIndexFootball.getLiveHall().getBlockCode(), live.getContentId(),
                            live.getContentType(), live.getEndTime());
                }
            }
            break;
            case R.id.fl_football_rec1: {
                if (liveList.size() > 1) {
                    BeanIndexFootball.LiveHallBean.ListBeanX live = liveList.get(1);
                    getAuthPlayUrl(live.getName(), beanIndexFootball.getLiveHall().getBlockCode(), live.getContentId(),
                            live.getContentType(), live.getEndTime());
                }
            }
            break;
            case R.id.fl_football_rec2: {
                if (liveList.size() > 2) {
                    BeanIndexFootball.LiveHallBean.ListBeanX live = liveList.get(2);
                    getAuthPlayUrl(live.getName(), beanIndexFootball.getLiveHall().getBlockCode(), live.getContentId(),
                            live.getContentType(), live.getEndTime());
                }
            }
            break;
            case R.id.fl_fight_rec_big:
                playFightRec(0);
                break;
            case R.id.ll_fight1:
                playFightRec(1);
                break;
            case R.id.ll_fight2:
                playFightRec(2);
                break;
            case R.id.ll_fight3:
                playFightRec(3);
                break;
            case R.id.fl_football_league_more:
                Intent intent = new Intent(MainActivity.this, LeagueMoreActivity.class);
                intent.putParcelableArrayListExtra("leagueList", leagueList);
                ActivityUtils.gotoActivityWithIntent(MainActivity.this, intent);
                break;
            case R.id.tv_more_fight: {
                Intent intent2 = new Intent(MainActivity.this, ScheduleMoreActivity.class);
                intent2.putExtra("folderCode", beanIndexFight.getMoreSchedule().getBlockCode());
                ActivityUtils.gotoActivityWithIntent(MainActivity.this, intent2);
            }
            break;
            case R.id.ll_meal: //我的套餐
                ActivityUtils.gotoActivity(MainActivity.this, MyPackageActivity.class);
                break;
            case R.id.ll_record: //我的记录
                ActivityUtils.gotoActivity(MainActivity.this, MyRecordActivity.class);
                break;
            case R.id.ll_guest: //我的竞猜
                ActivityUtils.gotoActivity(MainActivity.this, MyGuestActivity.class);
                break;
            case R.id.ll_business: //我的运营位置
                break;
        }
    }

    private void playFightRec(int index) {
        if (beanIndexFight == null) {
            return;
        }
        if (beanIndexFight.getRecommend() == null) {
            return;
        }
        if (beanIndexFight.getRecommend().getList() == null) {
            return;
        }
        if (beanIndexFight.getRecommend().getList().size() <= index) {
            return;
        }

        List<BeanIndexFight.RecommendBean.ListBeanX> recList = beanIndexFight.getRecommend().getList();
        BeanIndexFight.RecommendBean.ListBeanX rec0 = recList.get(index);
        getAuthPlayUrl(rec0.getName(), beanIndexFight.getRecommend().getBlockCode(), rec0.getContentId(),
                rec0.getContentType(), rec0.getEndTime());
    }

    //联赛的点击事件
    @OnClick({R.id.iv_football_league1, R.id.iv_football_league2, R.id.iv_football_league3, R.id.iv_football_league4,
            R.id.iv_football_league5, R.id.iv_football_league6, R.id.iv_football_league7, R.id.iv_football_league8, R.id.iv_football_league9})
    public void onLeagueClicked(View view) {
        if (leagueIVList.contains(view)) {
            int index = leagueIVList.indexOf(view);
            BeanIndexFootball.LeagueMatchListBean.ListBean bean = leagueList.get(index);
            Intent intent = new Intent(MainActivity.this, LeagueHomeDataActivity.class);
            intent.putExtra("League", bean);
            ActivityUtils.gotoActivityWithIntent(this, intent);
        }

    }

    //中间推荐的点击事件
    @OnClick({R.id.rl_football_rec1, R.id.rl_football_rec2, R.id.rl_football_rec3, R.id.tv_football_more_game,
            R.id.rl_fight_rec1, R.id.rl_fight_rec2, R.id.rl_fight_rec3, R.id.tv_more_fight})
    public void onRecClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_football_rec1:
                playtTdayRec(0, true);
//                showDialogTip("尊敬的用户：\n你还没订购本场比赛，是否现在购买", "去订购", "取消");
                break;
            case R.id.rl_football_rec2:
                playtTdayRec(1, true);
//                showDialogTip("尊敬的用户：\n你还没订购本场比赛，是否现在购买", "去订购", "取消");
                break;
            case R.id.rl_football_rec3:
                playtTdayRec(2, true);
                break;
            case R.id.tv_football_more_game: {
                Intent intent = new Intent(MainActivity.this, ScheduleMoreActivity.class);
                intent.putExtra("folderCode", beanIndexFootball.getMoreSchedule().getBlockCode());
                ActivityUtils.gotoActivityWithIntent(MainActivity.this, intent);
            }
            break;
            case R.id.rl_fight_rec1:
                playtTdayRec(0, false);
//                showDialogTip("尊敬的用户：\n你还没订购本场比赛，是否现在购买", "去订购", "取消");
                break;
            case R.id.rl_fight_rec2:
                playtTdayRec(1, false);
//                showDialogTip("尊敬的用户：\n你还没订购本场比赛，是否现在购买", "去订购", "取消");
                break;
            case R.id.rl_fight_rec3:
                playtTdayRec(2, false);
                break;
            case R.id.tv_more_fight: {
                Intent intent = new Intent(MainActivity.this, ScheduleMoreActivity.class);
                intent.putExtra("folderCode", beanIndexFootball.getMoreSchedule().getBlockCode());
                ActivityUtils.gotoActivityWithIntent(MainActivity.this, intent);
            }
            break;
        }
    }

    private void playtTdayRec(int index, boolean isFootball) {
        if (isFootball) {
            List<BeanIndexFootball.TodayRecommendBean.ListBeanXXX> todayList = beanIndexFootball.getTodayRecommend().getList();
            if (todayList.size() > index) {
                BeanIndexFootball.TodayRecommendBean.ListBeanXXX beanXXX = todayList.get(index);
                getAuthPlayUrl(beanXXX.getName(), "", beanXXX.getContentId(), beanXXX.getContentType(), beanXXX.getEndTime());
            }
        } else {
            List<BeanIndexFight.TodayRecommendBean.ListBeanXX> todayList = beanIndexFight.getTodayRecommend().getList();
            if (todayList.size() > index) {
                BeanIndexFight.TodayRecommendBean.ListBeanXX beanXXX = todayList.get(index);
                getAuthPlayUrl(beanXXX.getName(), "", beanXXX.getContentId(), beanXXX.getContentType(), beanXXX.getEndTime());
            }
        }
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (footRecContentList.contains(view)) {
            int index = footRecContentList.indexOf(view);
            //右侧推荐
            BeanIndexFootball.OperationPositionBean operationPositionBean = beanIndexFootball.getOperationPosition();
            //右侧推荐
            List<BeanIndexFootball.OperationPositionBean.ListBeanXX> positonList = operationPositionBean.getList();
            BeanIndexFootball.OperationPositionBean.ListBeanXX beanXX = positonList.get(index);

            if (beanXX.getMedia() != null) {
                getAuthPlayUrl(beanXX.getMedia().getName(), "", beanXX.getMedia().getContentId(), beanXX.getMedia().getContentType(),
                        beanXX.getMedia().getEndTime());
            }
        } else if (fightRecContentList.contains(view)) {
            int index = fightRecContentList.indexOf(view);
            BeanIndexFight.OperationPositionBean operationPositionBean = beanIndexFight.getOperationPosition();
            //右侧推荐
            List<BeanIndexFight.OperationPositionBean.ListBean> positonList = operationPositionBean.getList();
            BeanIndexFight.OperationPositionBean.ListBean listBean = positonList.get(index);
            if (listBean.getMedia() != null) {
                getAuthPlayUrl(listBean.getMedia().getName(), "", listBean.getMedia().getContentId(), listBean.getMedia().getContentType(),
                        listBean.getMedia().getEndTime());
            }
        }
    }

    @Override
    public void clickDialogLeft() {
        super.clickDialogLeft();
        ActivityUtils.gotoActivity(this, BuySingleMealActivity.class);
    }

    @Override
    public void onStart() {
        super.onStart();
        tvAd.startFlipping();
    }

    @Override
    public void onStop() {
        super.onStop();
        tvAd.stopFlipping();
    }

    @Override
    public void netBack(int requestTag, Object object) {
        super.netBack(requestTag, object);
        if (requestTag == ApiUtils.TAGIndex_football) {
            beanIndexFootball = (BeanIndexFootball) object;
            if (beanIndexFootball == null) {
                return;
            }
            //联赛
            leagueList = beanIndexFootball.getLeagueMatchList().getList();
            for (int i = 0; i < leagueList.size() && i < 9; i++) {
                BeanIndexFootball.LeagueMatchListBean.ListBean bean = leagueList.get(i);
                ImageToos.setImageWithUrl(bean.getLogoUrl(), leagueIVList.get(i), FootballApplication.getOptions(R.drawable.default_league));
            }
            for (int i = leagueList.size(); i < 9; i++) {
                leagueIVList.get(i).setVisibility(View.GONE);
            }
            if (leagueList.size() > 9) {
                flFootballLeagueMore.setVisibility(View.VISIBLE);
            } else {
                flFootballLeagueMore.setVisibility(View.GONE);
            }

            //中间直播的
            liveList = beanIndexFootball.getLiveHall().getList();
            if (liveList != null && liveList.size() > 0) {
                ImageToos.setImageWithUrl(liveList.get(0).getLivePicUrl(), ivFootballRec1, FootballApplication.getOptions(R.drawable.default_video));
                tvFootballRec1Title.setText(liveList.get(0).getName());
                if (liveList.size() > 1) {
                    ImageToos.setImageWithUrl(liveList.get(1).getLivePicUrl(), ivFootballRec2, FootballApplication.getOptions(R.drawable.default_video));
                    tvFootballRec2Title.setText(liveList.get(1).getName());
                }
            }

            //今日推荐
            List<BeanIndexFootball.TodayRecommendBean.ListBeanXXX> todayList = beanIndexFootball.getTodayRecommend().getList();

            if (todayList != null && todayList.size() > 0) {
                ImageToos.setImageWithUrl(todayList.get(0).getHomeTeamLogo(), ivFootballRec1LeftIcon, FootballApplication.getOptions(R.drawable.default_team));
                ImageToos.setImageWithUrl(todayList.get(0).getAwayTeamLogo(), ivFootballRec1RightIcon, FootballApplication.getOptions(R.drawable.default_team));
                tvFootballRec1LeftName.setText(todayList.get(0).getHomeTeamName());
                tvFootballRec1RightName.setText(todayList.get(0).getAwayTeamName());
                tvFootballRec1Time.setText(todayList.get(0).getStartTime());
                if (TextUtils.equals(todayList.get(0).getProgramType(), "1")) {
                    tvFootballRec1Type.setText("直播");
                    tvFootballRec1Type.setBackgroundResource(R.drawable.shape_red_corner);
                } else {
                    tvFootballRec1Type.setText("回看");
                    tvFootballRec1Type.setBackgroundResource(R.drawable.shape_blue_corner);
                }

                if (todayList.size() > 1) {
                    ImageToos.setImageWithUrl(todayList.get(1).getHomeTeamLogo(), ivFootballRec2LeftIcon, FootballApplication.getOptions(R.drawable.default_team));
                    ImageToos.setImageWithUrl(todayList.get(1).getAwayTeamLogo(), ivFootballRec2RightIcon, FootballApplication.getOptions(R.drawable.default_team));
                    tvFootballRec2LeftName.setText(todayList.get(1).getHomeTeamName());
                    tvFootballRec2RightName.setText(todayList.get(1).getAwayTeamName());
                    tvFootballRec2Time.setText(todayList.get(1).getStartTime());
                    if (TextUtils.equals(todayList.get(1).getProgramType(), "1")) {
                        tvFootballRec2Type.setText("直播");
                        tvFootballRec2Type.setBackgroundResource(R.drawable.shape_red_corner);
                    } else {
                        tvFootballRec2Type.setText("回看");
                        tvFootballRec2Type.setBackgroundResource(R.drawable.shape_blue_corner);
                    }
                }

                if (todayList.size() > 2) {
                    ImageToos.setImageWithUrl(todayList.get(2).getHomeTeamLogo(), ivFootballRec3LeftIcon, FootballApplication.getOptions(R.drawable.default_team));
                    ImageToos.setImageWithUrl(todayList.get(2).getAwayTeamLogo(), ivFootballRec3RightIcon, FootballApplication.getOptions(R.drawable.default_team));
                    tvFootballRec3LeftName.setText(todayList.get(2).getHomeTeamName());
                    tvFootballRec3RightName.setText(todayList.get(2).getAwayTeamName());
                    tvFootballRec3Time.setText(todayList.get(2).getStartTime());
                    if (TextUtils.equals(todayList.get(2).getProgramType(), "1")) {
                        tvFootballRec3Type.setText("直播");
                        tvFootballRec3Type.setBackgroundResource(R.drawable.shape_red_corner);
                    } else {
                        tvFootballRec3Type.setText("回看");
                        tvFootballRec3Type.setBackgroundResource(R.drawable.shape_blue_corner);
                    }
                }

            }

            //右侧推荐
            for (int i = 0; i < footRecContentList.size(); i++) {
                flFootballContent.removeView(footRecContentList.get(i));
            }
            footRecContentList.clear();
            BeanIndexFootball.OperationPositionBean operationPositionBean = beanIndexFootball.getOperationPosition();
            if (operationPositionBean != null) {
                //右侧推荐
                List<BeanIndexFootball.OperationPositionBean.ListBeanXX> positonList = operationPositionBean.getList();
                if (positonList != null) {
                    for (int i = 0; i < positonList.size(); i++) {
                        View itemRec = FrameLayout.inflate(this, R.layout.item_rec_big, null);
                        itemRec.setOnFocusChangeListener(this);
                        itemRec.setOnClickListener(this);
                        itemRec.setId(i);
                        if (i % 5 == 0) {
                            itemRec.setNextFocusRightId(i + 2);
                            itemRec.setNextFocusLeftId(i - 3);
                        }
                        FrameLayout.LayoutParams param = new FrameLayout.LayoutParams((int) getResources().getDimension(R.dimen.px360),
                                (int) getResources().getDimension(R.dimen.px420));
                        param.rightMargin = (int) getResources().getDimension(R.dimen.px100);
                        int itemMargin = (int) getResources().getDimension(R.dimen.px10);

                        switch (i % 5) {
                            case 0:
                                param.height = (int) getResources().getDimension(R.dimen.px420);
                                param.leftMargin = (int) getResources().getDimension(R.dimen.px1700) +
                                        (param.width + itemMargin) * (i / 5) * 2;
                                param.topMargin = (int) getResources().getDimension(R.dimen.px30);
                                itemRec.setNextFocusUpId(R.id.rb_football);
                                break;
                            case 1:
                                param.leftMargin = (int) getResources().getDimension(R.dimen.px1700) +
                                        (param.width + itemMargin) * (i / 5) * 2;
                                param.topMargin = (int) getResources().getDimension(R.dimen.px30) + (param.height + itemMargin);
                                param.height = (int) getResources().getDimension(R.dimen.px210);
                                break;
                            case 2:
                                param.height = (int) getResources().getDimension(R.dimen.px205);
                                param.leftMargin = (int) getResources().getDimension(R.dimen.px1700) +
                                        (param.width + itemMargin) * (i / 5 * 2 + 1);
                                param.topMargin = (int) getResources().getDimension(R.dimen.px30);
                                itemRec.setNextFocusUpId(R.id.rb_football);
                                break;
                            case 3:
                                param.height = (int) getResources().getDimension(R.dimen.px205);
                                param.leftMargin = (int) getResources().getDimension(R.dimen.px1700) +
                                        (param.width + itemMargin) * (i / 5 * 2 + 1);
                                param.topMargin = (int) getResources().getDimension(R.dimen.px245);
                                break;
                            case 4:
                                param.height = (int) getResources().getDimension(R.dimen.px210);
                                param.leftMargin = (int) getResources().getDimension(R.dimen.px1700) +
                                        (param.width + itemMargin) * (i / 5 * 2 + 1);
                                param.topMargin = (int) getResources().getDimension(R.dimen.px460);
                                break;

                        }


                        ImageView ivRec = (ImageView) itemRec.findViewById(R.id.iv_rec);
                        TextView tvRecTitle = (TextView) itemRec.findViewById(R.id.tv_rec_title);
                        BeanIndexFootball.OperationPositionBean.ListBeanXX posData = positonList.get(i);
                        if (posData.getMedia() != null) {
                            ImageToos.setImageWithUrl(posData.getMedia().getImages().get(0).getUrl(), ivRec,
                                    FootballApplication.getOptions(R.drawable.default_video));
                            tvRecTitle.setText(posData.getMedia().getName());
                        } else {
                            ImageToos.setImageWithUrl(posData.getPic().getPicUrl(), ivRec,
                                    FootballApplication.getOptions(R.drawable.default_video));
                            tvRecTitle.setText(posData.getPic().getName());
                        }
                        footRecContentList.add(itemRec);
                        flFootballContent.addView(itemRec, param);

                    }
                }

            }


        } else if (requestTag == ApiUtils.TAGIndex_grapple) {
            beanIndexFight = (BeanIndexFight) object;
            if (beanIndexFight == null) {
                return;
            }

            List<BeanIndexFight.RecommendBean.ListBeanX> recList = beanIndexFight.getRecommend().getList();

            if (recList != null && recList.size() > 0) {
                BeanIndexFight.RecommendBean.ListBeanX rec0 = recList.get(0);
                ImageToos.setImageWithUrl(rec0.getLivePicUrl(), ivFightRecBig, FootballApplication.getOptions(R.drawable.default_video));
                if (TextUtils.equals(rec0.getProgramType(), "1")) {
                    tvFightRecBigTitle.setText("直播. " + rec0.getName());
                } else {
                    tvFightRecBigTitle.setText("回看. " + rec0.getName());
                }

                if (recList.size() > 1) {
                    BeanIndexFight.RecommendBean.ListBeanX rec1 = recList.get(1);
                    if (TextUtils.equals(rec1.getProgramType(), "1")) {
                        tvFightRec1Type.setText("直播");
                    } else {
                        tvFightRec1Type.setText("回看");
                    }
                    tvFightTitle1.setText(rec1.getName());
                }

                if (recList.size() > 2) {
                    BeanIndexFight.RecommendBean.ListBeanX rec2 = recList.get(2);
                    if (TextUtils.equals(rec2.getProgramType(), "1")) {
                        tvFightRec2Type.setText("直播");
                    } else {
                        tvFightRec2Type.setText("回看");
                    }
                    tvFightTitle2.setText(rec2.getName());
                }

                if (recList.size() > 3) {
                    BeanIndexFight.RecommendBean.ListBeanX rec3 = recList.get(3);
                    if (TextUtils.equals(rec3.getProgramType(), "1")) {
                        tvFightRec3Type.setText("直播");
                    } else {
                        tvFightRec3Type.setText("回看");
                    }
                    tvFightTitle3.setText(rec3.getName());
                }

            }

            //今日推荐
            List<BeanIndexFight.TodayRecommendBean.ListBeanXX> todayFightList = beanIndexFight.getTodayRecommend().getList();

            if (todayFightList != null && todayFightList.size() > 0) {
                ImageToos.setImageWithUrl(todayFightList.get(0).getHomeTeamLogo(), ivFightRec1LeftIcon, FootballApplication.getOptions(R.drawable.default_team));
                ImageToos.setImageWithUrl(todayFightList.get(0).getAwayTeamLogo(), ivFightRec1RightIcon, FootballApplication.getOptions(R.drawable.default_team));
                tvFightRec1LeftName.setText(todayFightList.get(0).getHomeTeamName());
                tvFightRec1RightName.setText(todayFightList.get(0).getAwayTeamName());
                tvFightRec1Time.setText(todayFightList.get(0).getStartTime());
                if (TextUtils.equals(todayFightList.get(0).getProgramType(), "1")) {
                    tvFightRec1Type.setText("直播");
                    tvFightRec1Type.setBackgroundResource(R.drawable.shape_red_corner);
                } else {
                    tvFightRec1Type.setText("回看");
                    tvFightRec1Type.setBackgroundResource(R.drawable.shape_blue_corner);
                }

                if (todayFightList.size() > 1) {
                    ImageToos.setImageWithUrl(todayFightList.get(1).getHomeTeamLogo(), ivFightRec2LeftIcon, FootballApplication.getOptions(R.drawable.default_team));
                    ImageToos.setImageWithUrl(todayFightList.get(1).getAwayTeamLogo(), ivFightRec2RightIcon, FootballApplication.getOptions(R.drawable.default_team));
                    tvFightRec2LeftName.setText(todayFightList.get(1).getHomeTeamName());
                    tvFightRec2RightName.setText(todayFightList.get(1).getAwayTeamName());
                    tvFightRec2Time.setText(todayFightList.get(1).getStartTime());
                    if (TextUtils.equals(todayFightList.get(1).getProgramType(), "1")) {
                        tvFightRec2Type.setText("直播");
                        tvFightRec2Type.setBackgroundResource(R.drawable.shape_red_corner);
                    } else {
                        tvFightRec2Type.setText("回看");
                        tvFightRec2Type.setBackgroundResource(R.drawable.shape_blue_corner);
                    }
                }

                if (todayFightList.size() > 2) {
                    ImageToos.setImageWithUrl(todayFightList.get(2).getHomeTeamLogo(), ivFightRec3LeftIcon, FootballApplication.getOptions(R.drawable.default_team));
                    ImageToos.setImageWithUrl(todayFightList.get(2).getAwayTeamLogo(), ivFightRec3RightIcon, FootballApplication.getOptions(R.drawable.default_team));
                    tvFightRec3LeftName.setText(todayFightList.get(2).getHomeTeamName());
                    tvFightRec3RightName.setText(todayFightList.get(2).getAwayTeamName());
                    tvFightRec3Time.setText(todayFightList.get(2).getStartTime());
                    if (TextUtils.equals(todayFightList.get(2).getProgramType(), "1")) {
                        tvFightRec3Type.setText("直播");
                        tvFightRec3Type.setBackgroundResource(R.drawable.shape_red_corner);
                    } else {
                        tvFightRec3Type.setText("回看");
                        tvFightRec3Type.setBackgroundResource(R.drawable.shape_blue_corner);
                    }
                }
            }

            //右侧推荐
            for (int i = 0; i < fightRecContentList.size(); i++) {
                flFightContent.removeView(fightRecContentList.get(i));
            }
            fightRecContentList.clear();
            BeanIndexFight.OperationPositionBean operationPositionBean = beanIndexFight.getOperationPosition();
            if (operationPositionBean != null) {
                //右侧推荐
                List<BeanIndexFight.OperationPositionBean.ListBean> positonList = operationPositionBean.getList();

                if (positonList != null) {
                    for (int i = 0; i < positonList.size(); i++) {
                        View itemRec = FrameLayout.inflate(this, R.layout.item_rec_big, null);
                        itemRec.setOnFocusChangeListener(this);
                        itemRec.setOnClickListener(this);
                        itemRec.setId(i);
                        if (i % 5 == 0) {
                            itemRec.setNextFocusRightId(i + 2);
                            itemRec.setNextFocusLeftId(i - 3);
                        }
                        FrameLayout.LayoutParams param = new FrameLayout.LayoutParams((int) getResources().getDimension(R.dimen.px360),
                                (int) getResources().getDimension(R.dimen.px420));
                        param.rightMargin = (int) getResources().getDimension(R.dimen.px100);
                        int itemMargin = (int) getResources().getDimension(R.dimen.px10);

                        switch (i % 5) {
                            case 0:
                                param.height = (int) getResources().getDimension(R.dimen.px420);
                                param.leftMargin = (int) getResources().getDimension(R.dimen.px1280) +
                                        (param.width + itemMargin) * (i / 5) * 2;
                                param.topMargin = (int) getResources().getDimension(R.dimen.px30);
                                itemRec.setNextFocusUpId(R.id.rb_fight);
                                break;
                            case 1:
                                param.leftMargin = (int) getResources().getDimension(R.dimen.px1280) +
                                        (param.width + itemMargin) * (i / 5) * 2;
                                param.topMargin = (int) getResources().getDimension(R.dimen.px30) + (param.height + itemMargin);
                                param.height = (int) getResources().getDimension(R.dimen.px210);
                                break;
                            case 2:
                                param.height = (int) getResources().getDimension(R.dimen.px205);
                                param.leftMargin = (int) getResources().getDimension(R.dimen.px1280) +
                                        (param.width + itemMargin) * (i / 5 * 2 + 1);
                                param.topMargin = (int) getResources().getDimension(R.dimen.px30);
                                itemRec.setNextFocusUpId(R.id.rb_fight);
                                break;
                            case 3:
                                param.height = (int) getResources().getDimension(R.dimen.px205);
                                param.leftMargin = (int) getResources().getDimension(R.dimen.px1280) +
                                        (param.width + itemMargin) * (i / 5 * 2 + 1);
                                param.topMargin = (int) getResources().getDimension(R.dimen.px245);
                                break;
                            case 4:
                                param.height = (int) getResources().getDimension(R.dimen.px210);
                                param.leftMargin = (int) getResources().getDimension(R.dimen.px1280) +
                                        (param.width + itemMargin) * (i / 5 * 2 + 1);
                                param.topMargin = (int) getResources().getDimension(R.dimen.px460);
                                break;
                        }

                        ImageView ivRec = (ImageView) itemRec.findViewById(R.id.iv_rec);
                        TextView tvRecTitle = (TextView) itemRec.findViewById(R.id.tv_rec_title);
                        BeanIndexFight.OperationPositionBean.ListBean posData = positonList.get(i);
                        if (posData.getMedia() != null) {
                            ImageToos.setImageWithUrl(posData.getMedia().getImages().get(0).getUrl(), ivRec,
                                    FootballApplication.getOptions(R.drawable.default_video));
                            tvRecTitle.setText(posData.getMedia().getName());
                        } else {
                            ImageToos.setImageWithUrl(posData.getPic().getPicUrl(), ivRec,
                                    FootballApplication.getOptions(R.drawable.default_video));
                            tvRecTitle.setText(posData.getPic().getName());
                        }
                        fightRecContentList.add(itemRec);
                        flFightContent.addView(itemRec, param);
                    }
                }
            }
        } else if (requestTag == ApiUtils.TAGADData) {
            beanAd = (BeanAd) object;
            List<String> info = new ArrayList<>();
            if (beanAd != null && !TextUtils.isEmpty(beanAd.getNoticeTitle())) {
                info.add(beanAd.getNoticeTitle());
            }
            // 在代码里设置自己的动画
            tvAd.startWithList(info, R.anim.anim_bottom_in, R.anim.anim_top_out);
        }
    }

    private void getAdData() {
        Map<String, String> params = new HashMap<>();
        params.put("requestType", "4");
        params.put("command", "2");
        NetUtils.postJsonData(this, ApiUtils.UCS_URL, ApiUtils.TAGADData, params, BeanAd.class, null, this);
    }


}
